const express = require("express");
const cors = require("cors");
const e = require("express");
const app = express();
const port = 3000;
app.use(cors());
const charactersnumber = "123456789"
const charactersstring = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
function generateRandomData(characters) {
  const data = [];

  const charactersLength = characters.length;

  for (let i = 0; i < 10; i++) {
    const randomKey =
      characters.charAt(Math.floor(Math.random() * charactersLength)) +
      characters.charAt(Math.floor(Math.random() * charactersLength)) +
      characters.charAt(Math.floor(Math.random() * charactersLength));
    const randomValue =
      characters.charAt(Math.floor(Math.random() * charactersLength)) +
      characters.charAt(Math.floor(Math.random() * charactersLength)) +
      characters.charAt(Math.floor(Math.random() * charactersLength));
    data.push({
      value: randomValue,
      key: randomKey,
      id: i + 1,
    });
  }

  return data;
}
app.get("/api/data", (req, res) => {

  if(req.query.value === "1") {
    const randomData = generateRandomData(charactersnumber);
    res.json({msg:1,data:randomData});
  }else{
    const randomData = generateRandomData(charactersstring);
    res.json({msg:2,data:randomData});
  }

});

app.listen(port, () => {
  console.log(`Server is running at http://localhost:${port}`);
});
